Linking Complex Variables (Aliasing)
This topic explains how complex variables can become linked to each other and how to prevent that from happening when not desired. The behavior of simple and complex variables differ in this regard. This topic provides examples and instructions on how to prevent linkage between complex variables and lists of complex variables.
Example with Simple Variables
Below is a simple workflow.
The workflow steps perform the following actions:
-
NumVar_A is set to 1.
-
NumVar_B is set to NumVar_A.
-
NumVar_A is changed from 1 to 400.
After the workflow is executed, NumVar_A has a value of 400, and NumVar_B has a value of 1. In other words, in step 2, the value of NumVar_B was set to the value of NumVar_A, but no connection was created between the two variables. Modifying NumVar_A afterwards had no effect onNumVar_B .
Assignment of one simple variable to another simple variable does not create any connection between the two variables.
Example with Complex Variables
Below is a simple workflow.
The workflow steps perform the following actions:
-
John Smith's details are entered in the variable Customer_A, which is a complex variable of the user-defined type Customer.
-
The complex variable Customer_B is set to Customer_A.
-
The values of Customer_A are replaced with Mary Winter's details instead of John Smith's.
After the workflow is executed, Customer_B has the same values as Customer_A (Mary Winter's details), even though when Customer_B was set to Customer_A, Customer_A stored John Smith's details.
When assigning one complex variable (the source) to another (the target), the target is linked to the source and its values will always be the same as the values in the target. This behavior is called aliasing. In some cases such linkage is desired, in other cases it is not.
Preventing Linkage (Aliasing)
To prevent a target complex variable from becoming linked to the source variable when using the Assign method, use the Duplicate function from the Variables built-in service.
When assigning a duplicate of a source variable to a target variable, the target variable receives the values of the source variable, but does not become linked to the source variable.
The example shown above is modified below.
Below is the modified workflow. Only the second step is modified.
The workflow steps perform the following actions:
-
John Smith's details are entered in the variable Customer_A, which is a complex variable of the user-defined type Customer.
-
The complex variable Customer_B is set to a duplicate of Customer_A, using the Duplicate function.
-
The values of Customer_A are replaced with Mary Winter's details instead of John Smith's.
After the workflow is executed, Customer_B stores John Smith's details (as were originally entered into Customer_A), while Customer_A stores its modified values of Mary Winter.
Using the Duplicate function prevents the target variable from becoming linked to the source variable.
Preventing Linkage (Aliasing) in Lists
When adding a complex variable to a list of complex variables, for example, when using the Add Element at End method, the new element in the list is linked to variable.
In the example below, the variable Customer_B is added to the list List_of_Customers.
The list will initially store the values stored in Customer_B, but if the values of Customer_B are modified later on, the values stored in the list will be modified too.
To prevent such linkage, use the Duplicate function from the Variables built-in service, as shown below.
Using the Duplicate function prevents a target element in a complex list from becoming linked to source complex variable.